<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="UTF-8">
  <title>单点登录</title>
</head>
<body>
<div>
  <h2>SSO 单点登录</h2>
  <input id="service" th:value="${service}" type="hidden">
  <input id="username" autocomplete="off"><br>
  <input type="password" id="password" autocomplete="off"><br>
  <button onclick="login()">登录</button>
</div>
<script>
  function login() {
    var username = document.getElementById("username").value
    var password = document.getElementById("password").value
    //创建核心对象
    xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    //编写回调函数
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        //alert(xhr.responseText)
        var data = JSON.parse(xhr.responseText)
        if (data.code != 0) {
          alert(data.msg)
        } else {
          alert(data.msg)
          //login success
          var service = document.getElementById("service").value
          window.location.href = service + '?sso_token=' + data.data
        }
      }
    }
    //open设置请求方式和请求路径
    xhr.open("POST", "/ssoLogin");//一个servlet，后面还可以写是否同步
    //设置请求头
    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded")
    //send 发送
    xhr.send("username=" + username + "&password=" + password);
  }
</script>
</body>
</html>
